package com.ruoyi.legal.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Holiday;
import com.ruoyi.legal.service.IHolidayService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 节假日值班Controller
 *
 * @author xwj
 * @date 2025-05-06
 */
@RestController
@RequestMapping("/legal/holiday")
@Api(tags = "节假日值班")
public class HolidayController extends BaseController {
    @Autowired
    private IHolidayService holidayService;

    /**
     * 查询节假日值班列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Holiday holiday) {
        startPage();
        List<Holiday> list = holidayService.selectHolidayList(holiday);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Holiday holiday) {
        startOrderBy();
        List<Holiday> list = holidayService.selectHolidayList(holiday);
        return success(list);
    }

    /**
     * 导出节假日值班列表
     */
    @Log(title = "节假日值班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Holiday holiday) {
        List<Holiday> list = holidayService.selectHolidayList(holiday);
        ExcelUtil<Holiday> util = new ExcelUtil<Holiday>(Holiday.class);
        util.exportExcel(response, list, "节假日值班数据");
    }

    /**
     * 获取节假日值班详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(holidayService.selectHolidayById(id));
    }

    /**
     * 新增节假日值班
     */
    @Log(title = "节假日值班", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Holiday holiday) {
        SysUser user = getLoginUser().getUser();
        if (holiday.getId() == null) {
            holiday.setCreateId(user.getUserId());
            holiday.setCreateTime(DateUtils.getNowDate());
            holiday.setCreateBy(user.getNickName());
            holidayService.insertHoliday(holiday);
        } else {
            holiday.setUpdateId(user.getUserId());
            holiday.setUpdateTime(DateUtils.getNowDate());
            holiday.setUpdateBy(user.getNickName());
            holidayService.updateHoliday(holiday);
        }

        return success(holiday);
    }

    /**
     * 修改节假日值班
     */
    @Log(title = "节假日值班", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Holiday holiday) {
        SysUser user = getLoginUser().getUser();
        holiday.setUpdateId(user.getUserId());
        holiday.setUpdateTime(DateUtils.getNowDate());
        holiday.setUpdateBy(user.getNickName());
        return success(holidayService.updateHoliday(holiday));
    }

    /**
     * 删除节假日值班
     */
    @Log(title = "节假日值班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(holidayService.deleteHolidayByIds(ids));
    }

    @Log(title = "节假日值班", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Holiday holiday) {
        int num = holidayService.delete(holiday);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Holiday>> queryPage(@RequestBody Holiday holiday) {
        Pages<Holiday> page = holidayService.queryPage(holiday);
        return ResponseModel.of(page);
    }

    @PostMapping("/countNum")
    @ApiOperation("统计数量")
    public ResponseModel countNum(@RequestBody Holiday holiday) {
        List<Map> list = holidayService.countNum(holiday);
        return ResponseModel.of(list);
    }
    @PostMapping("/yearHolidays")
    @ApiOperation("某年的节假日")
    public ResponseModel yearHolidays(@RequestBody Holiday holiday) {
        List<Map> list = holidayService.yearHolidays(holiday);
        return ResponseModel.of(list);
    }
}
